$(function () {
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    var token = localStorage.token;
    if (!token) {
        location.href = "../loginAndReg/login.html";
        return false;
    }
    new FastClick(document.body);
    $(".phone").html(localStorage.userMobile);
    $(".userName").val(localStorage.nickname);
    $(".showHeadPic").attr("src", localStorage.userHeadPic);
    if (parseInt(localStorage.gender) === 1) {
        $(".selectGender").html("男");
        $(".selectGender").attr("data-gender", 1);
    } else if (parseInt(localStorage.gender) === 2) {
        $(".selectGender").html("女");
        $(".selectGender").attr("data-gender", 2);
    } else if (!localStorage.gender) {
        $(".selectGender").html("请选择");
    }
    if (localStorage.birthday === "undefined" || !localStorage.birthday) {
        $(".date").html("请选择");
        $(".date").attr("data-date", 0);
    } else {
        $(".date").html(localStorage.birthday);
        $(".date").attr("data-date", 1);
    }
    var fontSize = $(".userName").css("font-size");
    var width = $(".userName").val().length * parseFloat(fontSize);
    $(".userName").css("width", width);
    $(".userName").on("input", function () {
        var width = $(this).val().length * parseFloat(fontSize);
        $(this).css('width', width);
    });
    //上传头像
    $("#hPic").on("change", function () {
        var headPic = this.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(headPic);
        reader.onload = function () {
            $(".showHeadPic").attr("src", reader.result);
            var face = reader.result.split(",")[1];
            $.ajax({
                type: "post",
                url: domain.test + "/uploadheadpic",
                // url: domain.online + "/member/face",
                data: { face: face },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("token", token);
                },
                success: function (response) {
                    console.log(response);
                    if (response.data.rtnCode === 200) {
                        // if(response.rtnCode===200) {
                        localStorage.userHeadPic = response.data.data;
                        // localStorage.userHeadPic = response.data;
                        $(".showHeadPic").attr("src", response.data.data);
                        // $(".showHeadPic").attr("src",response.data);
                    }
                }
            });
        }
    })
    //修改性别
    $(".gender").on("click", function () {
        var width = window.screen.width;
        var height = window.screen.height;
        $(".logoutMarsk").css({
            "width": width,
            "height": height,
            "display": "block"
        });
    });
    $(".cancel").on("click", function () {
        $(".selectGender").html("女");
        $(".selectGender").attr("data-gender", 2);
        $(".logoutMarsk").css("display", "none");
    });
    $(".sure").on("click", function () {
        $(".selectGender").html("男");
        $(".selectGender").attr("data-gender", 1);
        $(".logoutMarsk").css("display", "none");
    });
    //修改资料
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".save").on("click", function () {
        var nickname = $(".userName").val();
        if (!nickname) {
            tips("用户昵称不能为空");
            return false;
        }
        var sex = parseInt($(".selectGender").attr("data-gender"));
        if (!sex) {
            tips("请选择你的性别");
            return false;
        }
        var birthdayStatus = parseInt($(".date").attr("data-date"));
        if (birthdayStatus === 0) {
            tips("请选择您的生日");
            return false;
        }
        var birthday = $(".date").html();
        $.ajax({
            type: "post",
            url: domain.test + "/editmsg",
            // url: domain.online + "/member/save",
            data: {
                nickname: nickname,
                sex: sex,
                birthday: birthday
            },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                // if (response.rtnCode === 200) {
                    $(".loadingMarsk").css("display","none");
                    localStorage.nickname = nickname;
                    localStorage.gender = sex;
                    tips("保存成功");
                    setTimeout(function () {
                    location.href = "./me.html";                    
                    }, 1000);
                }
                if (response.data.rtnCode === 401 || response.data.rtnCode === 400) {
                    // if (response.rtnCode === 401 || response.rtnCode === 400) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
    //跳转绑定新手机
    $(".bindPhone").on("click", function () {
        location.href = "./bindNewPhone.html";
    });
});